﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]//挂脚本的时候自动挂上刚体组件
public class D06_RigidBody0 : MonoBehaviour
{
    private Rigidbody rig;
    public float force = 100;

    private void Start()
    {
        rig = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        float v = Input.GetAxis("Vertical");
        rig.AddForce(Vector3.forward * v * Time.deltaTime * force);
    }
}

[RequireComponent(typeof(Rigidbody))]//挂脚本的时候自动挂上刚体组件
public class D06_RigidBody2 : MonoBehaviour
{
    private Rigidbody rig;
    public float force = 100;
    private void Start()
    {
        rig = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        float v = Input.GetAxis("Vertical");

        //添加扭力
        rig.AddRelativeTorque(Vector3.right * v * Time.deltaTime * force);

        //添加爆炸力

        if (Input.GetKeyDown(KeyCode.Space))
        {
            rig.AddExplosionForce(force, transform.position, 10);
        }
    }
}

//通过速度来让刚体运动,只要设置一个速度刚体就会沿这个速度一直运动,除非摩擦和碰撞才会停下来
public class D06_RigidBody : MonoBehaviour
{
    private Rigidbody rig;
    public float speed = 5f;
    public float force = 100;
    private void Start()
    {
        rig = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        float v = Input.GetAxis("Vertical");
        rig.velocity = transform.forward * v * speed;
    }
}